package edu.hit.pact.pgse.crawler.bbs;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Map;

public class TianYaBBSURLFilter {
	
	private ArrayList<BBSURL> bbsUrls ;
	private ArrayList<BBSURL> postUrls;
	private BBSURL nextPageUrl;
	private String topicName = null;
	private int pageno = 0;
	private TianYaBBSURL nowPage;
	
	protected static String HOST = "www.tianya.cn";
	protected static int POSTURLLENGTH = 6;
	protected static int NEXTPAGELENGTH = 4;

	
	public TianYaBBSURLFilter() {
		super();
	}



	public TianYaBBSURLFilter(ArrayList<BBSURL> bbsUrls) {
		super();
		this.bbsUrls = bbsUrls;
	}
	
	
	
   public TianYaBBSURLFilter(ArrayList<BBSURL> bbsUrls, TianYaBBSURL nowPage) {
		super();
		this.bbsUrls = bbsUrls;
		this.nowPage = nowPage;
		this.nowPage.extractQuery();
		this.nowPage.extractPath();
		this.nowPage.fllter();
		this.topicName = this.nowPage.getSubitem();
		this.pageno = this.nowPage.getPageno();
	}
   


   public TianYaBBSURLFilter(ArrayList<BBSURL> bbsUrls, String topicName,
			int pageno) {
		super();
		this.bbsUrls = bbsUrls;
		this.topicName = topicName;
		this.pageno = pageno;
	}

   public ArrayList<BBSURL> getPostUrls() {
		return postUrls;
	}

	public BBSURL getNextPageUrl() {
		return nextPageUrl;
	}

	public void setPageno(int pageno) {
		this.pageno = pageno;
	}

	public ArrayList<BBSURL> getBbsUrls() {
		return bbsUrls;
	}


	public void setBbsUrls(ArrayList<BBSURL> bbsUrls) {
		this.bbsUrls = bbsUrls;
	}


	public String getTopicName() {
		return topicName;
	}


	public void setTopicName(String topicName) {
		this.topicName = topicName;
	}


	//http://www.tianya.cn/new/techforum/articleslist.asp?pageno=5&iditem=99&part=0&nextarticle=2010%2D11%2D3+8%3A13%3A09&subitem=
	public void filter()
	{
	   for(BBSURL bbsUrl : bbsUrls){
		   bbsUrl.extractQuery();
		   bbsUrl.extractPath();
		   
		   if(bbsUrl.getUrl().getHost() != HOST)
			   continue;
		   //String[] params = bbsUrl.getParams();
		   String[] paths = bbsUrl.getPaths();
		   if(paths.length != POSTURLLENGTH && paths.length != NEXTPAGELENGTH)
			   continue;
			if (paths.length == POSTURLLENGTH) {
				if (!paths[3].equals(topicName) || !paths[2].equals("content"))
					continue;
				postUrls.add(bbsUrl);
			}else if(paths.length == NEXTPAGELENGTH){
				Map params = bbsUrl.getParams();
				if((params.containsKey("iditem") && params.get("iditem").equals(topicName)) || (params.containsKey("stritem") && params.get("stritem").equals(topicName)) ){
				    if(params.containsKey("pageno") && Integer.parseInt((String) params.get("pageno")) == pageno + 1 ){
				    	nextPageUrl = bbsUrl;
				    }
				}
				
			}
		   
	   }
		
	}
	public boolean isTianyaBbsArticleUrl(URL oneUrl,String subitem)
	{
		BBSURL bbsUrl = new BBSURL(oneUrl);
		bbsUrl.extractQuery();
		bbsUrl.extractPath();
		   
		if(!bbsUrl.getUrl().getHost().equals(HOST))
			 return false;
		   //String[] params = bbsUrl.getParams();
		String[] paths = bbsUrl.getPaths();
		if(paths.length != POSTURLLENGTH )
			return false;
		if (paths.length == POSTURLLENGTH) {
			if (!paths[3].equals(subitem) || !paths[2].equals("content"))
				return false;
			return true;
		}
				
		return false;
	}

	public boolean isTinayaNextPageUrl(URL oneUrl, int pageno, String subitem) {
		String tmp = oneUrl.toString();
		if(tmp.contains("pageno")){
			int i=0;
			i++;
		}
		BBSURL bbsUrl = new BBSURL(oneUrl);
		if (!bbsUrl.getUrl().getHost().equals(HOST))
			return false;
		bbsUrl.extractQuery();
		Map map = bbsUrl.getParams();
		
		boolean flag1 = map.containsKey("pageno")&& (Integer.parseInt((String) map.get("pageno")) == pageno + 1);
		boolean flag2 = map.containsKey("iditem") && map.get("iditem").equals(subitem);
		boolean flag3 = map.containsKey("stritem") && map.get("stritem").equals(subitem);
//		System.out.println(flag1);
//		System.out.println(flag2);
//		System.out.println(flag3);
//		System.out.println(Integer.parseInt((String) map.get("pageno")));
//		System.out.println(pageno);
		
		if (flag1 && (flag2 || flag3 )) {
			return true;
		}
		return false;
	}
	
	public static void main(String[] args){
		try {
			URL oneUrl = new URL("http://www.tianya.cn/publicforum/articleslist/0/develop.shtml");
			TianYaBBSURLFilter filter = new TianYaBBSURLFilter();
			//System.out.println(filter.filterOneUrl(oneUrl));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
	
